//
//  XCStatusCell.swift
//  小陈微博Swift用纯代码创建
//
//  Created by 小陈 on 16/3/2.
//  Copyright © 2016年 小陈. All rights reserved.
//

import UIKit

import SnapKit

class XCStatusCell: UITableViewCell {
    
    private var bottomViewConstrains: Constraint?
    

    var status :XCStatus?{
        didSet{
            originalView.status = status
            
            // 判断是否有转发微博
            bottomViewConstrains?.uninstall()
            if let _ = status?.retweeted_status {
                // 有转发微博数据
                retweetedView.status = status?.retweeted_status
               retweetedView.hidden = false
                statusButtonView.snp_makeConstraints { (make) -> Void in
                    self.bottomViewConstrains = make.top.equalTo(retweetedView.snp_bottom).constraint
                    
                }
                
            }else{
                // 没有转发微博数据
                   retweetedView.hidden = true
                statusButtonView.snp_makeConstraints { (make) -> Void in
                    self.bottomViewConstrains = make.top.equalTo(originalView.snp_bottom).constraint
                    
                }
            }
        }
    }
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
       backgroundColor = UIColor(white: 237/255.0, alpha: 1)
           // 准备UI 控件
             prepareUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareUI(){
        // 内容的约束
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(statusButtonView)
        
        
        // 原创微博
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
        }
        
        // 转发微博
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
//            make.size.equalTo(CGSizeMake(200, 200))
        }
        
        
        // 底部约束的View
        statusButtonView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
        self.bottomViewConstrains = make.top.equalTo(retweetedView.snp_bottom).constraint
            
        }
   
        
        
        // contentView 的约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(statusButtonView.snp_bottom)
        }
    }
 
    
      // 原创微博
   private lazy var originalView:XCStatusOriginalView = XCStatusOriginalView()
    /// 底部的View
    private lazy var statusButtonView:XCStatusButtomView = XCStatusButtomView()

// 转发微博
    private lazy var retweetedView:XCStatusRetweetedView = XCStatusRetweetedView()
    
}
